<?php

namespace app\store\model;

use app\common\model\Activity as ActivityModel;
use Lvht\GeoHash;

/**
 * 文章模型
 * Class Article
 * @package app\store\model
 */
class Activity extends ActivityModel
{
    /**
     * 获取活动列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this->with(['image', 'team','user'])
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);

    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传相关图片';
            return false;
        }
        if (empty($data['activity_content'])) {
            $this->error = '请输入活动详情';
            return false;
        }
        $data['theme_label'] = implode(',', $data['theme_label']);
        $data['wxapp_id'] = self::$wxapp_id;
        $coordinate = explode(',', $data['coordinate']);
        $data['latitude'] = $coordinate[0];
        $data['longitude'] = $coordinate[1];
        // 生成geohash
        $Geohash = new Geohash;
        $data['geohash'] = $Geohash->encode($data['longitude'], $data['latitude']);
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
//        if (empty($data['image_id'])) {
//            $this->error = '请上传相关图片';
//            return false;
//        }
//        if (empty($data['activity_content'])) {
//            $this->error = '请输入活动详情';
//            return false;
//        }
        $data['theme_label'] = implode(',', $data['theme_label']);
        $coordinate = explode(',', $data['coordinate']);
        $data['latitude'] = $coordinate[0];
        $data['longitude'] = $coordinate[1];
        // 生成geohash
        $Geohash = new Geohash;
        $data['geohash'] = $Geohash->encode($data['longitude'], $data['latitude']);
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

}